{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:12:42.069337Z",
     "start_time": "2024-09-20T06:12:41.517469500Z"
    }
   },
   "id": "955def2dff9332a5",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:12:43.087058800Z",
     "start_time": "2024-09-20T06:12:42.054826Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  age_range  gender\n0   376517        6.0     1.0\n1   234512        5.0     0.0\n2   344532        5.0     0.0\n3   186135        5.0     0.0\n4    30230        5.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>376517</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>234512</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>344532</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>186135</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>30230</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type\n0   328862   323294     833       2882    2661.0         829            0\n1   328862   844400    1271       2882    2661.0         829            0\n2   328862   575153    1271       2882    2661.0         829            0\n3   328862   996875    1271       2882    2661.0         829            0\n4   328862  1086186    1271       1253    1049.0         829            0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>seller_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>328862</td>\n      <td>323294</td>\n      <td>833</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>328862</td>\n      <td>844400</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>328862</td>\n      <td>575153</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>328862</td>\n      <td>996875</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>328862</td>\n      <td>1086186</td>\n      <td>1271</td>\n      <td>1253</td>\n      <td>1049.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log = pd.read_csv('user_log_format1.csv')\n",
    "user_log.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:05.452664400Z",
     "start_time": "2024-09-20T06:12:43.003002700Z"
    }
   },
   "id": "2c525cb895b58ac5",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id         0\nage_range    2217\ngender       6436\ndtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:06.894535500Z",
     "start_time": "2024-09-20T06:14:05.224607100Z"
    }
   },
   "id": "2e9d2aa998b4b3b9",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id            0\nitem_id            0\ncat_id             0\nseller_id          0\nbrand_id       91015\ntime_stamp         0\naction_type        0\ndtype: int64"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.isnull().sum() "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:18.964258700Z",
     "start_time": "2024-09-20T06:14:06.309049300Z"
    }
   },
   "id": "b52a126a0523a131",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:20.797973300Z",
     "start_time": "2024-09-20T06:14:18.993778Z"
    }
   },
   "id": "7dffd89587d5fc0f",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 2.9 GB\n"
     ]
    }
   ],
   "source": [
    "user_log.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:20.960079600Z",
     "start_time": "2024-09-20T06:14:20.795471500Z"
    }
   },
   "id": "a57b928566bc0c2e",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(54925330, 7)"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:21.614013500Z",
     "start_time": "2024-09-20T06:14:20.823991300Z"
    }
   },
   "id": "78ef6fdac1a8b2ed",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10476\\1355905500.py:2: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10476\\1355905500.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['gender'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id      0\nage_range    0\ngender       0\ndtype: int64"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除空值\n",
    "user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
    "user_info['gender'].replace(np.nan,-1,inplace=True)\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:21.766614500Z",
     "start_time": "2024-09-20T06:14:21.037631500Z"
    }
   },
   "id": "fb4b8f869391b193",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10476\\1715364757.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_log['brand_id'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nseller_id      0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log['brand_id'].replace(np.nan,-1,inplace=True)\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:14:24.217238500Z",
     "start_time": "2024-09-20T06:14:21.150706100Z"
    }
   },
   "id": "26c9ebd5dccdc3c3",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "13750198\n"
     ]
    }
   ],
   "source": [
    "print(user_info.duplicated().sum())\n",
    "print(user_log.duplicated().sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:15:26.814149700Z",
     "start_time": "2024-09-20T06:14:24.203730600Z"
    }
   },
   "id": "b05a9703db44e02a",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.drop_duplicates(inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:22.088839800Z",
     "start_time": "2024-09-20T06:15:26.813650400Z"
    }
   },
   "id": "ea2c96927e2f314b",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label\n0    34176         3906      0\n1    34176          121      0\n2    34176         4356      1\n3    34176         2217      0\n4   230784         4818      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('train_format1.csv')\n",
    "train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:22.411053400Z",
     "start_time": "2024-09-20T06:16:22.096344600Z"
    }
   },
   "id": "75111d5dd5f7e736",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 随机采样\n",
    "sample_fraction = 0.2  # 采样比例\n",
    "train = train.sample(frac=sample_fraction, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:22.909382100Z",
     "start_time": "2024-09-20T06:16:22.404550500Z"
    }
   },
   "id": "db085989732786bb",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender\n0   399620          310      0        3.0     1.0\n1   183656         3129      0        2.0     1.0\n2   214005         3609      0        0.0     0.0\n3    76778         2824      0        0.0     1.0\n4   258043         4760      1        0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>183656</td>\n      <td>3129</td>\n      <td>0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>214005</td>\n      <td>3609</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>76778</td>\n      <td>2824</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>258043</td>\n      <td>4760</td>\n      <td>1</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(train,user_info, on='user_id')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:23.310648400Z",
     "start_time": "2024-09-20T06:16:22.557651500Z"
    }
   },
   "id": "a4a491529289015f",
   "execution_count": 16
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 选取的特征\n",
    "用户的年龄(age_range)\n",
    "用户的性别(gender)\n",
    "某用户在该商家日志的总条数(total_logs)\n",
    "用户浏览的商品的数目，就是浏览了多少个商品(unique_item_ids)\n",
    "浏览的商品的种类的数目，就是浏览了多少种商品(categories)\n",
    "用户浏览的天数(browse_days)\n",
    "用户单击的次数(one_clicks)\n",
    "用户添加购物车的次数(shopping_carts)\n",
    "用户购买的次数(purchase_times)\n",
    "用户收藏的次数(favourite_times)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f372c652392c9afc"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:23.341168400Z",
     "start_time": "2024-09-20T06:16:22.901876500Z"
    }
   },
   "id": "357535d4b544c316",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 某用户在该商家日志的总条数(total_logs)\n",
    "total_logs = user_log.groupby([user_log['user_id'],user_log['merchant_id']]).count().reset_index()[['user_id','merchant_id','item_id']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:49.524615Z",
     "start_time": "2024-09-20T06:16:22.933398Z"
    }
   },
   "id": "f1ca6388f423e4f9",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  total_logs\n0        1          471           1\n1        1          739           1\n2        1          925           2\n3        1         1019           2\n4        1         1156           1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>total_logs</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_logs.rename(columns={\"item_id\":\"total_logs\"},inplace=True)\n",
    "total_logs.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:49.594661100Z",
     "start_time": "2024-09-20T06:16:49.533120500Z"
    }
   },
   "id": "47f95ad6cda6838",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender  total_logs\n0   399620          310      0        3.0     1.0           5\n1   183656         3129      0        2.0     1.0           3\n2   214005         3609      0        0.0     0.0           8\n3    76778         2824      0        0.0     1.0           3\n4   258043         4760      1        0.0     0.0           2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>183656</td>\n      <td>3129</td>\n      <td>0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>214005</td>\n      <td>3609</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>76778</td>\n      <td>2824</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>258043</td>\n      <td>4760</td>\n      <td>1</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(df_train,total_logs,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:17:01.323387400Z",
     "start_time": "2024-09-20T06:16:49.589657Z"
    }
   },
   "id": "216f675c529a3679",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 用户浏览的商品的数目(unique_item_ids)\n",
    "unique_item_ids_tmp = user_log.groupby([user_log['user_id'],user_log['merchant_id'],user_log['item_id']]).count().reset_index()[['user_id','merchant_id']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:17:49.882149400Z",
     "start_time": "2024-09-20T06:17:01.326389400Z"
    }
   },
   "id": "90c231817110ead8",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id\n0        1          471\n1        1          739\n2        1          925\n3        1         1019\n4        1         1156",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_item_ids_tmp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:17:49.928180300Z",
     "start_time": "2024-09-20T06:17:49.888654700Z"
    }
   },
   "id": "9624fd79706bbae3",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "unique_item_ids_tmp['cnt'] = 1\n",
    "unique_item_ids = unique_item_ids_tmp.groupby([unique_item_ids_tmp[\"user_id\"],unique_item_ids_tmp[\"merchant_id\"]]).count().reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:01.285546900Z",
     "start_time": "2024-09-20T06:17:49.917175800Z"
    }
   },
   "id": "e64085029a4781d8",
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  unique_item_ids\n0        1          471                1\n1        1          739                1\n2        1          925                1\n3        1         1019                1\n4        1         1156                1\n5        1         2245                4\n6        1         4026                1\n7        1         4177                1\n8        1         4335                1\n9        2          420               15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>unique_item_ids</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_item_ids.rename(columns={\"cnt\":\"unique_item_ids\"},inplace=True)\n",
    "unique_item_ids.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:01.334079300Z",
     "start_time": "2024-09-20T06:18:01.289049100Z"
    }
   },
   "id": "f26a49d9cd2eef0a",
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender  total_logs  unique_item_ids\n0   399620          310      0        3.0     1.0           5                4\n1   183656         3129      0        2.0     1.0           3                2\n2   214005         3609      0        0.0     0.0           8                2\n3    76778         2824      0        0.0     1.0           3                2\n4   258043         4760      1        0.0     0.0           2                1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>5</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>183656</td>\n      <td>3129</td>\n      <td>0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>214005</td>\n      <td>3609</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>76778</td>\n      <td>2824</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>258043</td>\n      <td>4760</td>\n      <td>1</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(df_train,unique_item_ids,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:13.225978100Z",
     "start_time": "2024-09-20T06:18:01.335585200Z"
    }
   },
   "id": "998af52818e103cf",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 浏览的商品的种类的数目(categories)\n",
    "categories = user_log.groupby([user_log[\"user_id\"],user_log[\"merchant_id\"],user_log[\"cat_id\"]]).count().reset_index()[[\"user_id\",\"merchant_id\",\"item_id\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:40.897847800Z",
     "start_time": "2024-09-20T06:18:13.228979700Z"
    }
   },
   "id": "26bf1982420f3667",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  categories\n0        1          471           1\n1        1          739           1\n2        1          925           2\n3        1         1019           2\n4        1         1156           1\n5        1         2245           4\n6        1         4026           3\n7        1         4177           1\n8        1         4335           1\n9        2          420          12",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>categories</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>12</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories.rename(columns={\"item_id\":\"categories\"},inplace=True)\n",
    "categories.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:40.950382800Z",
     "start_time": "2024-09-20T06:18:40.902350200Z"
    }
   },
   "id": "e7556a835e678ed8",
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender  total_logs  \\\n0   399620          310      0        3.0     1.0           5   \n1   399620          310      0        3.0     1.0           5   \n2   399620          310      0        3.0     1.0           5   \n3   183656         3129      0        2.0     1.0           3   \n4   214005         3609      0        0.0     0.0           8   \n\n   unique_item_ids  categories  \n0                4           1  \n1                4           3  \n2                4           1  \n3                2           3  \n4                2           8  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids</th>\n      <th>categories</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>5</td>\n      <td>4</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>5</td>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>399620</td>\n      <td>310</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>5</td>\n      <td>4</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>183656</td>\n      <td>3129</td>\n      <td>0</td>\n      <td>2.0</td>\n      <td>1.0</td>\n      <td>3</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>214005</td>\n      <td>3609</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n      <td>2</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.merge(df_train,categories,on=['user_id','merchant_id'],how='left')\n",
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:49.392452500Z",
     "start_time": "2024-09-20T06:18:40.936374300Z"
    }
   },
   "id": "c60a095c15849cac",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "# df_train = pd.merge(df_train,user_log,on=['user_id','merchant_id'],how='left')\n",
    "# df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:49.404959800Z",
     "start_time": "2024-09-20T06:18:49.392952600Z"
    }
   },
   "id": "f55104e9f1c1a701",
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "label\n0    89785\n1     8538\nName: count, dtype: int64"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['label'].value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:49.560062200Z",
     "start_time": "2024-09-20T06:18:49.409463Z"
    }
   },
   "id": "92134a1df9e4dcf8",
   "execution_count": 30
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "29215edd592ddbbf"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:49.586579600Z",
     "start_time": "2024-09-20T06:18:49.497521700Z"
    }
   },
   "id": "980c1b62af9c4d18",
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X = df_train.drop('label',axis=1)\n",
    "X = X.drop('user_id',axis=1)\n",
    "y = df_train['label']\n",
    "X_train,X_val,y_train,y_val = train_test_split(X, y, test_size=0.7, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:52.895525100Z",
     "start_time": "2024-09-20T06:18:49.515532500Z"
    }
   },
   "id": "231aba80b2fab444",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import roc_auc_score"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:54.799804100Z",
     "start_time": "2024-09-20T06:18:52.904531100Z"
    }
   },
   "id": "a1d1ebf5db12cef1",
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 在不设置 class_weight 的情况下，随机森林默认给予所有样本相同的权重。如果数据集不平衡，模型可能会偏向于多数类。 设置 class_weight='balance'：这会根据类别频率自动调整权重，使得模型在训练时更加关注少数类。具体来说，权重是 n_samples / (n_classes * np.bincount(y))，其中 n_samples 是样本总数，n_classes 是类别数，np.bincount(y) 是每个类别的样本数。\n",
    "model = RandomForestClassifier(max_depth=10,min_samples_split=3,min_samples_leaf=2, random_state=0,class_weight='balanced')\n",
    "model.fit(X_train,y_train)\n",
    "y_pred=model.predict(X_val)\n",
    "y_proba = model.predict_proba(X_val)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:58.868115300Z",
     "start_time": "2024-09-20T06:18:54.816815Z"
    }
   },
   "id": "d678f0a05dc415b3",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证集auc:  0.6723706017671016\n"
     ]
    }
   ],
   "source": [
    "auc = roc_auc_score(y_val,y_pred)\n",
    "print('验证集auc: ',auc)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:58.966218100Z",
     "start_time": "2024-09-20T06:18:58.872618400Z"
    }
   },
   "id": "afc91b7279bb790f",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集auc:  0.7610861245650786\n"
     ]
    }
   ],
   "source": [
    "y_preds=model.predict(X_train)\n",
    "auc = roc_auc_score(y_train,y_preds)\n",
    "print('训练集auc: ',auc)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T11:05:05.194922400Z",
     "start_time": "2024-09-20T11:05:04.422410100Z"
    }
   },
   "id": "c0b8e31711f651f5",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "merchant_id: 0.2528845628578883\n",
      "age_range: 0.08150882059780183\n",
      "gender: 0.05106567309512702\n",
      "total_logs: 0.2677236824340281\n",
      "unique_item_ids: 0.26524468267552864\n",
      "categories: 0.08157257833962618\n"
     ]
    }
   ],
   "source": [
    "# 打印特征重要性\n",
    "for name, importance in zip(X_train, model.feature_importances_):\n",
    "    print(f\"{name}: {importance}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:59.084796500Z",
     "start_time": "2024-09-20T06:18:58.984729800Z"
    }
   },
   "id": "2413f997a26ec5ba",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:59.097304600Z",
     "start_time": "2024-09-20T06:18:59.045770700Z"
    }
   },
   "id": "a2c3dec9ff1ea314",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:18:59.101306900Z",
     "start_time": "2024-09-20T06:18:59.063783Z"
    }
   },
   "id": "a79e389c30e4b712",
   "execution_count": 35
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
